<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Core_ErrorController extends Zend_Controller_Action
{

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');

        $this->view->addScriptPath(APPLICATION_PATH . '/modules/core/views/scripts/');
        $this->view->exception = $errors->exception;
        $this->view->request   = $errors->request;

        switch ($errors->type) { 
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
                // 404 error -- controller, action or route not found
                $this->render404();
                break;
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
                // Application exceptions
                $this->applicationException();
                break;
            default:
                // application error 
                $this->render500();
                break;
        }

        if ('development' == APPLICATION_ENV) {
            $this->render('error');
        }
    }

    private function render404()
    {
        $this->getResponse()->setHttpResponseCode(404);
        $this->render('404');
    }

    private function render500()
    {
        $this->logException();

        $this->getResponse()->setHttpResponseCode(500);
        $this->render('500');
    }

    private function renderUnauthorized()
    {
        // Error 401 - Unauthorized
        $this->getResponse()->setHttpResponseCode(401);

        $this->view->authService = $this->_helper->authenticator();

        $this->render('unauthorized');
    }

    private function renderMissingArguments()
    {
        // Error 400 - Bad request
        $this->getResponse()->setHttpResponseCode(400);

        $this->render('missing-arguments');
    }

    private function applicationException() {
        $exception = $this->view->exception;
        if ($exception instanceof RedCross_Exception_Unauthorized) {
            $this->renderUnauthorized();
        } else if ($exception instanceof RedCross_Exception_ValidationFailed) {
            $this->renderMissingArguments();
        } else if ($exception instanceof RedCross_Exception_NotFound) {
            $this->render404();
        } else if ($exception instanceof RedCross_Exception_NotPost) {
            $this->render500();
        } else if ($exception instanceof RedCross_Exception) {
            $this->render500();
        } else if ($exception instanceof Zend_Exception) {
            $this->render500();
        } else if ($exception instanceof Doctrine_Exception) {
            $this->render500();
        } else {
            $this->render500();
        }
    }

    private function logException()
    {
        if (Zend_Registry::isRegistered('log')) {
            $log = Zend_Registry::get('log');
            $exception = $this->view->exception;
            $log->crit('Exception: ' . get_class($exception) . ' in file "' . $exception->getFile() .
                    '" on line ' . $exception->getLine() . ' with message "' . $exception->getMessage() . '"');
        }
    }

}

